package com.island.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.island.model.*;
import com.island.service.*;
import org.apache.java.security.*;

@Controller
@RequestMapping("/")
public class TestController {

	@Autowired
	private TestService testService;
	
	@RequestMapping(value = "/findall")
	public String myList(HttpServletRequest request,
			HttpServletResponse response) {
		List<Admin> areaList = testService.findBySql();
		request.setAttribute("adminList", areaList);
		return "list";
	}

	@RequestMapping(value = "/deleteadmin")
	public String deleteadmin(HttpServletRequest request,
			HttpServletResponse response) {
		int id = new Integer(request.getParameter("id"));
		testService.deleteAdmin(id);

		return myList(request, response);
	}

	@RequestMapping(value = "/updateadmin")
	public String updateadmin(HttpServletRequest request,
			HttpServletResponse response) {
		String password = request.getParameter("password");
		int id = new Integer(request.getParameter("id"));
		Admin admin = new Admin();
		admin.setLoginid(id);
		admin.setLoginpassword(password);
		testService.updateAdmin(admin);

		return myList(request, response);
	}

	@RequestMapping(value = "/addadmin")
	public String addadmin(HttpServletRequest request,
			HttpServletResponse response) {
		MD5 md5 = new MD5();
		String password = md5.MD5File(request.getParameter("password"));
		String name = request.getParameter("name");
		Admin admin = new Admin();
		admin.setLoginname(name);
		admin.setLoginpassword(password);
		testService.insertAdmin(admin);
		return myList(request, response);
	}
}
